﻿using Janus.BaseEntitys;
using Janus.Cores.Extension;
using Janus.Cores.Linq;
using Janus.Mes.DTO;
using Janus.Mes.Services.Interface;
using Janus.Mes.VO;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;

namespace Janus.Mes.WebClient.Areas.BaseData.Controllers.DefectReason
{
    public class DefectReasonController : BaseController
    {
        #region 列表
        /// <summary>
        /// 缺陷描述首页（返回数据列表）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Index()
        {
            //return View(GetList(new FormCollection()));
            return View(new List<DefectReasonVO>());
        }

        /// <summary>
        /// 返回查询数据
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            return View(GetList(collection));
        }

        #endregion

        #region 新增修改
        [HttpGet]
        public ActionResult Create(string id)
        {
            DefectReasonVO defectreason = null;
            ViewBag.IsCreate = true;

            var _listDefectL = DefectLevelServices.FindAll(new Querier());
            var list = new List<DefectLevelVO>();
            if(_listDefectL!=null)
            {
                list = _listDefectL.ToEntitys<DefectLevelVO>();
            }
            ViewData["DefectLevel"] = list ?? new List<DefectLevelVO>();
            if (!string.IsNullOrEmpty(id))
            {
                ViewBag.IsCreate = false;
                Querier query = new Querier(" DefectRCode={@0} ", new List<object>() { id });
                var model = DefectReasonServices.FindOne(query);
                defectreason = model.ToEntity<DefectReasonVO>();
            }
            else
            {
                defectreason = base.InitModel<DefectReasonVO>(defectreason);
            }
            return View(defectreason);
        }

        /// <summary>
        /// 新增系统参数
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public string Create(FormCollection form)
        {
            try
            {
                var result = false;
                DefectReasonVO model = new DefectReasonVO();
                model = base.SetModel<DefectReasonVO>(form);
                var defectreason = model.ToEntity<DefectReasonDTO>();
                result = DefectReasonServices.Save(defectreason) > 0;
                if (result)
                {
                    ajaxStr = base.AjaxResultSucess;
                }
                else
                {
                    ajaxStr = base.AjaxResultFail;
                }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = base.AjaxResultFail;
            }
            return "{" + ajaxStr + "}";
        }
        #endregion

        #region 主键是否存在
        [HttpPost]
        public string IsExist()
        {
            string data = Request["data"];
            Querier query = new Querier(" DefectRCode={@0} ", new List<object>() { data });
            bool result = DefectReasonServices.IsExists(query);
            if (!result)
            {
                ajaxStr = base.AjaxResultSucess;
            }
            else
            {
                ajaxStr = base.AjaxResultFail;
            }
            return "{" + ajaxStr + "}";
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除系统参数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public string Delete(object id)
        {
            try
            {
                var result = DefectReasonServices.Delete(id);
                if (result)
                {
                    ajaxStr = base.AjaxDeleteSucess;
                }
                else
                {
                    ajaxStr = base.AjaxResultFail;
                }
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = base.AjaxResultFail;
            }

            return "{" + ajaxStr + "}";
        }
        #endregion

        #region 私有方法
        protected IEnumerable<DefectReasonVO> GetList(FormCollection collection)
        {
            var searchParma = base.SearchPara<DefectReasonDTO>(collection);
            if (searchParma.Model.DefectRCode.IsNotEmpty())
            {
                searchParma.Queriers.Where("DefectRCode.Contains({@0})", searchParma.Model.DefectRCode);
            }
            if (searchParma.Model.DefectRName.IsNotEmpty())
            {
                searchParma.Queriers.Where("DefectRName.Contains({@0})", searchParma.Model.DefectRName);
            }
            //可以继续判断 增加查询条件

            var result = base.FindAll<DefectReasonDTO>(searchParma, DefectReasonServices);
            if (!result.ErrorMessage.IsNotEmpty())
            {
                var list = result.Result.ToEntitys<DefectReasonVO>();// AutoMapper.Mapper.DynamicMap<List<DefectReasonVO>>(result.Result);
                return list;
            }
            else
            {
                return new List<DefectReasonVO>();
            }
        }

        public ActionResult Details(string id)
        {
            DefectReasonVO DefectReason = null;
            if (!string.IsNullOrEmpty(id))
            {
                Querier query = new Querier("DefectRCode={@0}", new List<object>() {id });
                var model = DefectReasonServices.FindOne(query);
                DefectReason = model.ToEntity<DefectReasonVO>();
            }
            else
            {
                DefectReason = base.InitModel<DefectReasonVO>(DefectReason);
            }
            return View(DefectReason);
        }
        #endregion

    }

}

